local awaitExhaustedSkill = fk.CreateSkill{
  name = "iberia__await_exhausted_skill",
}

Fk:loadTranslationTable{
  ["iberia__await_exhausted"] = "以逸待劳",
  ["#iberia__await_exhausted-break"] = "以逸待劳：是否结束当前回合？"
}

local I = require "packages/iberia/utility"

awaitExhaustedSkill:addEffect("cardskill", {
  prompt = "#iberia__await_exhausted_skill",
  mod_target_filter = function(self, player, to_select, selected)
    return I.isMate(player, to_select)
  end,
  can_use = function(self, player, card)
    if player:prohibitUse(card) then return end
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      if not player:isProhibited(p, card) and self:modTargetFilter(player, p, {}, card, true) then
        return true
      end
    end
  end,
  on_use = function(self, room, use)
    for _, p in ipairs(room:getAlivePlayers()) do
      if not use.from:isProhibited(p, use.card) and not use:hasTarget(p) and self:modTargetFilter(use.from, p, {}, use.card, true) then
        use:addTarget(p)
      end
    end
  end,
  on_effect = function(self, room, effect)
    local target = effect.to
    if target.dead then return end
    target:drawCards(2, "iberia__await_exhausted")
    if target.dead then return end
    room:askToDiscard(target, {min_num = 2, max_num = 2, include_equip = true,
      skill_name = "iberia__await_exhausted", cancelable = false
    })
    if target == effect.from and I.isCounsellor(target) then
      room.logic:getCurrentEvent():findParent(GameEvent.UseCard):addExitFunc(function ()
        if target:isAlive() and room.current == target then
          if room:askToSkillInvoke(target, { skill_name = "iberia__await_exhausted", prompt = "#iberia__await_exhausted-break" } ) then
            room:endTurn()
          end
        end
      end)
    end
  end,
})

return awaitExhaustedSkill
